<template>
	<view class="content">
		<heade :title="'下'+title" cross bg_color="bg_fff" style="border-bottom: 2rpx solid #F8F8F8;">
			<!-- <text class="iconfont icon-erweima f-s20 f-wbold"></text> -->
		</heade>
		<view class="" v-if="!classify">
			<view class="" style="height: 200rpx;overflow: hidden;transition: .5s;" :style="{'height':isStatus? '200rpx': '800rpx'}">
				<!-- 客户 -->
				<view class="d-flex align-center p-b14 m-t14" style="border-bottom: 2rpx solid #f6f6f6;">
					<view class="d-flex position-relative" style="width: 220rpx;">
						<view class="m-l3 position-absolute" style="color: red;top: 60%;transform: translate(0,-50%);left: 6rpx;">*</view>
						<text class="m-l20 f-s14">客户</text>
					</view>
					<view class="d-flex align-center flex1 justify-content-space-between" @tap="$common.url('/pages/parameters/parameters?status=true')">
						<view class="f-s15 flex1 f-wbold webkit-line-clamp-1">{{client.name}}</view>
						<text class="iconfont icon-31xiala f-s15 m-r5" style="color: #dfdfdf;transform: rotate(270deg);"></text>
					</view>
				</view>
				<!-- 备注 -->
				<view class="d-flex align-center p-b14 m-t14" style="border-bottom: 2rpx solid #f6f6f6;">
					<view class="d-flex" style="width: 220rpx;">
						<text class="m-l20 f-s14">备注</text>
					</view>
					<input type="text" class="f-s15 flex1" v-model="Memo" placeholder="请输入备注">
				</view>
				<!-- 应收余额 -->
				<view class="d-flex align-center justify-content-space-between p-b14 m-t14" style="border-bottom: 2rpx solid #f6f6f6;">
					<view class="d-flex position-relative" style="width: 220rpx;">
						<text class="m-l20 f-s14">应收余额</text>
					</view>
					<view class="d-flex align-center justify-content-space-between flex1">
						<view class="uni-input f-s15  f-wbold">{{Number(client.arBalance).toFixed(2)}}</view>
						<text class="iconfont icon-31xiala f-s15 m-r5" style="color: #dfdfdf;transform: rotate(270deg);"></text>
					</view>
				</view>
				<!-- 仓库 -->
				<view class="d-flex align-center justify-content-space-between p-b14 m-t14" style="border-bottom: 2rpx solid #f6f6f6;">
					<view class="d-flex position-relative" style="width: 220rpx;">
						<text class="m-l20 f-s14">仓库</text>
					</view>
					<picker @change="wareIndexChange" class="flex1" :value="wareIndex" range-key="Name" :range="warehouse">
						<view class="d-flex align-center">
							<view class="uni-input f-s15 flex1 f-wbold">{{warehouse[wareIndex].Name}}</view>
							<text class="iconfont icon-31xiala f-s15 m-r5" style="color: #dfdfdf;transform: rotate(270deg);"></text>
						</view>
					</picker>
				</view>
				
				<!-- 单据日期 -->
				<view class="d-flex align-center p-b14 m-t14" style="border-bottom: 2rpx solid #f6f6f6;">
					<view class="d-flex position-relative" style="width: 220rpx;">
						<view class="m-l3 position-absolute" style="color: red;top: 60%;transform: translate(0,-50%);left: 6rpx;">*</view>
						<text class="m-l20 f-s14">单据日期</text>
					</view>
					<view class="width100 flex1" @click="dateOpen('VoucherDate', VoucherDate)">
						<text class="text1" v-if="VoucherDate">{{ VoucherDate }}</text>
						<text class="text1 co_999 f-s15" v-else>请选择单据日期</text>
					</view>
					<my-datetime ref="dateTimePop" :shownum="3" @ok="timeOk" ></my-datetime>
				</view>
				
				<!-- 结算客户 -->
				<view class="d-flex align-center justify-content-space-between p-b14 m-t14" style="border-bottom: 2rpx solid #f6f6f6;">
					<view class="d-flex position-relative" style="width: 220rpx;">
						<view class="m-l3 position-absolute" style="color: red;top: 60%;transform: translate(0,-50%);left: 6rpx;">*</view>
						<text class="m-l20 f-s14">结算客户</text>
					</view>
					<view class="d-flex align-center flex1 justify-content-space-between" @tap="$common.url('/pages/parameters/parameters?status=true&title=结算客户')">
						<view class="f-s15 flex1 webkit-line-clamp-1" :class="closeAccount.name == '请选择结算客户'?'co_grey':'f-wbold'">{{closeAccount.name}}</view>
						<text class="iconfont icon-31xiala f-s15 m-r5" style="color: #dfdfdf;transform: rotate(270deg);"></text>
					</view>
				</view>
				<!-- 到货地址 -->
				<view class="d-flex align-center p-b14 m-t14" style="border-bottom: 2rpx solid #f6f6f6;">
					<view class="d-flex position-relative" style="width: 220rpx;">
						<text class="m-l20 f-s14">到货地址</text>
					</view>
					<view class=""  v-if="client.partnerAddresDTOs" >
						<AddressPicker v-if="!client.partnerAddresDTOs[0].shipmentAddress" @change="changeClick" :level="3" class="f-s15" :class="!client.partnerAddresDTOs[0].shipmentAddress?'co_999':''">{{client.partnerAddresDTOs[0].shipmentAddress || '请选择地址'}} </AddressPicker>
						<view class="f-s15 webkit-line-clamp-1" v-else>{{client.partnerAddresDTOs[0].shipmentAddress}}</view>
					</view>
				</view>
				<!-- 支付方式 -->
				<view v-if="title != '销售订单'" class="d-flex align-center p-b14 m-t14" style="border-bottom: 2rpx solid #f6f6f6;">
					<view class="d-flex position-relative" style="width: 220rpx;">
						<text class="m-l20 f-s14">支付方式</text>
					</view>
					<view class="d-flex align-center flex1 justify-content-space-between" @tap="$common.url('/pages/parameters/department')">
						<view class="f-s15 flex1 f-wbold">{{modePayment}}</view>
						<text class="iconfont icon-31xiala f-s15 m-r5" style="color: #dfdfdf;transform: rotate(270deg);"></text>
					</view>
				</view>
			</view>
			
			
			<view class="d-flex align-center justify-content-center m-b10 f-s14 wanzxinx m-t10" @tap="isStatus = !isStatus">
				<view class="f-s12">完整信息</view>
				<view class="iconfont icon-sanjiaoxing f-s8 m-l5" style="transition: .5s;" :style="{'transform':isStatus?'rotate(0)':'rotate(180deg)'}"></view>
			</view>
			
			<!-- 商品列表 -->	
			<view class="bg_F3F2F7 p-t10 p-b10">
				<view class="f-s15 m-l10">商品列表</view>
				<view class="d-flex align-center m-t5">
					<view class="sousuo bg_fff flex1  d-flex align-center m-l10">
						<view class="d-flex align-center p-l10" @tap="classify = !classify" style="border-right: 2rpx solid #999;height: 60rpx;width: 120rpx;">
							<view class="f-s14">分类</view>
							<view class="iconfont icon-sanjiaoxing f-s8 m-l10"></view>
						</view>
						<input type="text" class="f-s14 m-l10 flex1" v-if="index == 0" v-model="Name" placeholder="搜索商品名称">
						<input type="text" class="f-s14 m-l10 flex1" v-else v-model="Code" placeholder="搜索商品编码">
					</view>
					<view class="iconfont icon-sousuo co_5493C7 f-wbold m-l10 f-s24 m-r10" @tap="getData"></view>
				</view>
			</view>
			
			<view class="m-b5">
				<view class="m-t50 text-center " v-if="goodsList.length == 0">
					<view class="iconfont icon-gouwuche f-s50"></view>
					<view class="f-s14 m-t10 co_666">您还没有选存货~</view>
				</view>
				<view class="p-b8 m-t10 p-l15" style="border-bottom: 10rpx solid  #F3F2F7;" v-for="(item,i) in goodsList" @tap="inventoryClick(item)">
					<view class="d-flex align-center">
						<!-- <view class="iconfont f-s20" @tap.stop="addClick(item,i)" :class="item.isStatus?'icon-danxuankuang-xuanzhong co_64acff':'icon-danxuankuang'"></view> -->
						<view class="iconfont f-s20 icon-gouwuche co_FF0000 f-wbold" @tap.stop="addClick(item,i)"></view>
						<view class="m-l15 flex1 m-r20">
							<view class="f-s14 f-wbold webkit-line-clamp-1">[{{item.code}}] {{item.name}}</view>
							<view class="d-flex align-center justify-content-space-between f-s14 m-t5">
								<view class="">可退：<text class="">{{item.priuserdefdecm7 == 1?'是':'否'}}</text></view>
								<view class="f-s12 d-flex align-items-center " >
									<view class="co_000">单价:</view>
									<view class="p-l5" style="width: 170rpx;" >
										<view class="f-s14  box_box co_E04C46">￥{{item.Price}}</view>
									</view>
								</view>
							</view>
							<view class="d-flex align-center justify-content-space-between">
								<view class="d-flex align-center m-t10 f-s14 position-relative" v-if="item.partnerUnitPrice">
									<view class="">销售单位:</view>
									<view class="" v-for="(item2,k) in item.partnerUnitPrice">
										<view class="d-flex align-center m-l10">
											<view @tap.stop="unitsClick(item2,i,k,item)" class="daiClass d-flex align-center justify-content-center border-radius-8 m-r10" :class="item2.count?'countClass':''">{{item2.UnitName}}</view>
										</view>
									</view>
								</view>
								<view class="">
									<view class="d-flex align-center justify-content-end m-t10">
										<view class="yuanxing d-flex bg_3A8CFF co_fff align-center justify-content-center" @tap.stop="jianClick(item,i,item.number)"><view class="iconfont icon-jianhao f-wbold f-s14"></view></view>
										<view class="m-l10 m-r10 text-center" style="min-width: 80rpx;border: 2rpx solid #C7CED5;">{{item.number}}</view>
										<view class="yuanxing d-flex bg_3A8CFF co_fff align-center justify-content-center" @tap.stop="jiaClick(item,i,item.number)"><view class="iconfont icon-jiahao_o f-wbold f-s14"></view></view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="b-liuhai" ><view style="height: 80rpx;"></view></view>
			<view style="position: fixed; bottom: 0;width: 100%;z-index: 9;" class="bg_fff">
				<view class="b-liuhai">
					<view class="bg_3A8CFF d-flex align-center justify-content-center" style="height: 80rpx;" @tap="submit">
						<view class="co_fff f-s14 f-w400">购物车</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="" v-if="classify" class=" box_box bg_fff p-b10" style="border-top: 2rpx solid #e8e8e8;width: 100%;z-index: 9;overflow-y: auto;">
			<view class="f-s14">
				<view class="m-l12  p-t10 p-b10" @tap="subset()">全部</view>
				<view class="" >	
					<view v-for="(item,i) in kehuList">
						<view class="p-l20 p-t10 p-b10 bg_f6f6f6" @click="kehuClick(i,item)">{{item.name}}({{item.sub.length}})</view>
						<view class="" v-for="(item2,j) in item.sub">
							<view class="p-l30 p-t10 p-b10 p-t10 p-b10 bg_fafafa"  @click="stairClick(i,j,item2)" v-if="item.stair">{{item2.name}}</view>
							<view class="p-l40 p-t10 p-b10 p-t10 p-b10 bg_fafafa" @tap="subset(item3)" v-if="item2.headset" v-for="item3 in item2.sub">{{item3.name}}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="b-liuhai" ><view style="height: 92rpx;"></view></view>
			<view style="position: fixed; bottom: 0;width: 100%;z-index: 9;" class="bg_fff" >
				<view class="b-liuhai d-flex  align-center" style="">
					<view class="f-w500 f-s17 text-center box_box bg_fff" style="width: 50%; height: 92rpx;line-height: 92rpx;border-top: 2rpx solid #DFDFDF;" @tap="classify = false">返回</view>
					<view class="f-w500 co_fff f-s17 text-center bg_4494EE	" style="width: 50%; height: 92rpx;line-height: 92rpx;" @tap="classify = false">收起</view>
				</view>
			</view>
		</view>
		
		<wPopup :character="character" :slide="slide" @wSlide="slide = false" @wClose="disposition">
			<view class="bg_fff p-b15 p-t15 p-l15  p-r10">
				<view class="f-s12 m-b8">共{{SaleOrderDetails.length}}项</view>
				<view class="f-s14 d-flex align-center flex1 justify-content-space-between m-t5" v-for="(item2,i) in SaleOrderDetails">
					<view class="f-s14 f-wbold">{{item2.Name}}</view>
					<view class="d-flex align-center">
						<view class="co_DF0902 m-r45">{{item2.OrigTaxPrice}}</view>
						<view class="d-flex align-center">
							<view class="inputClass d-flex-center text-center f-s14">{{item2.Quantity}}</view>
						</view>
					</view>
				</view>
			</view>
		</wPopup>
		
		<view class="" style="position: fixed;right: 20px;bottom:300rpx;z-index: 999;" v-if="scrollTopTap == true">
			<view class="iconfont icon-fanhuidingbu co_000 f-s32" @tap="pageScrollTo" ></view>
		</view>
	</view>
</template>

<script>
	import {
		executeSQL,
	} from '@/common/sqlite.js';
	import myDatetime from '@/components/my-datetime/my-datetime'
	import AddressPicker from "@/components/lingdang-AddressPicker/AddressPicker.vue"
	export default {
		data() {
			return {
				index:0,
				character:false,
				slide:false,
				classify:false,
				wareIndex:7,
				array:['商品名称','商品编码'],
				isStatus:true,
				hopeBeginTime: '',
				dateKey: '',
				planLocate:'',
				goodsList:[],
				warehouse:[{Name:'主仓'},{Name:'主仓'},{Name:'主仓'},{Name:'主仓'},{Name:'主仓'},{Name:'主仓'},{Name:'主仓'},{Name:'周转仓'}],
				count:0,
				unitList:[],
				SaleOrderDetails:[],
				client:{PartnerAddresDTOs:[{ShipmentAddress:''}]},
				VoucherDate:'',
				Memo:'',
				total:'0.00',
				Name:'',
				Code:'',
				closeAccount:{name:'请选择结算客户'},
				title:'销售订单',
				section:'1111',
				modePayment:'',
				kehuList:[],
				InventoryClass:{
					Code:'',
					Name:''
				},
				more:false,
				pageIndex:1,
				status:'noMore',
				valueStatus:'',
				salesmanName:{},
				scrollTopTap:false
			}
		},
		components: {
		  'my-datetime': myDatetime,
		  AddressPicker
		},
		onLoad(e) {
			this.VoucherDate = this.$common.timeToDate()
			if(e.client) {
				this.client = JSON.parse(e.client)
				this.closeAccount = JSON.parse(e.client)
			}
			if(e.title) {
				this.title = e.title
			}
			// 获取仓库数据
			executeSQL('xiaoap', 'select * from warehouse', res => {
				this.warehouse = res
				this.warehouse.forEach((e,i) => {
					if(e.Code == this.$db.get('default_config').chargeback_warehouse_code) {
						this.wareIndex = i
					}
				})
			})
			// 获取商品分类数据
			executeSQL('xiaoap', 'select * from goodsList2', res => {
				res.forEach(e => {
					e.sub = JSON.parse(e.sub)
				})
				let value = res
				value.forEach((e,i) => {
					e.stair = false
					value[i].sub.forEach(j => {
						j.headset = false
					})
				})
				this.kehuList = value
			})
			// this.getData()
			var networkType = ''
			let _this = this
			uni.getNetworkType({
			    success: function (res) {
					networkType = res.networkType
					if(networkType == 'none' && _this.$db.get('salesmanName')) {
						_this.salesmanName = _this.$db.get('salesmanName')
						console.log(_this.salesmanName.personName)
					}else {
						_this.getUserInfo()
					}
			    }
			});
		},
		onPageScroll(e) {
			if(e.scrollTop > 2500) {
				this.scrollTopTap = true
			}else {
				this.scrollTopTap = false
			}
		},
		onReachBottom(){
			var _this = this;
		   if(_this.status == 'loading'){
				_this.pageIndex++
				_this.more=true;
				this.getData()
		   }
		},
		onShow() {
			let _this = this
			var pages = getCurrentPages();
			var pages1 = pages[pages.length - 1]
			if(pages1.$vm.client) {
				this.client = pages1.$vm.client
				this.closeAccount.name = this.client.settlementPartner.name
				let list5 = this.goodsList
				list5.forEach(e => {
					if(e.partnerUnitPrice) {
						e.partnerUnitPrice.forEach(j => {
							if(this.client.priceGrade) {
								if(this.client.priceGrade.name == '普通客户价') {
									j.Price = j.PriceObj.retailPrice
								}else if(this.client.priceGrade.name.indexOf('一级') != -1) {
									j.Price = j.PriceObj.invSCost1
								}else if(this.client.priceGrade.name.indexOf('二级') != -1) {
									j.Price = j.PriceObj.invSCost2
								}else if(this.client.priceGrade.name.indexOf('三级') != -1) {
									j.Price = j.PriceObj.invSCost3
								}else if(this.client.priceGrade.name.indexOf('四级') != -1) {
									j.Price = j.PriceObj.invSCost4
								}else if(this.client.priceGrade.name.indexOf('五级') != -1) {
									j.Price = j.PriceObj.invSCost5
								}else if(this.client.priceGrade.name.indexOf('六级') != -1) {
									j.Price = j.PriceObj.invSCost6
								}else if(this.client.priceGrade.name.indexOf('最低') != -1) {
									j.Price = j.PriceObj.invLSPrice
								}else if(this.client.priceGrade.name.indexOf('会员价') != -1) {
									j.Price = j.PriceObj.memberPrice
								}else if(this.client.priceGrade.name.indexOf('采购价') != -1) {
									j.Price = j.PriceObj.purchasePrice
								}
							}
						})
						if(e.partnerUnitPrice.length == 2) {
							if(this.$db.get('default_config').unit == 2) {
								e.partnerUnitPrice[1].count = true
								e.partnerUnitPrice[0].count = false
								e.Price = e.partnerUnitPrice[1].Price
								e.RetailPriceNew = e.partnerUnitPrice[1].RetailPriceNew
							}else {
								e.partnerUnitPrice[0].count = true
								e.partnerUnitPrice[1].count = false
								e.Price = e.partnerUnitPrice[0].Price
								e.RetailPriceNew = e.partnerUnitPrice[0].RetailPriceNew
							}
						}
					}
				})
				this.goodsList = list5
			}
			if(pages1.$vm.closeAccount) {
				this.closeAccount = pages1.$vm.closeAccount
			}
			if(pages1.$vm.modePayment) {
				this.modePayment = pages1.$vm.modePayment
			}
		},
		onBackPress(options) {
			// #ifdef APP-PLUS
			if(this.aaa){
			　　let _this = this
				uni.showModal({
				title: '温馨提示',
				content: '您确定要返回上一页吗',
					success: function (res) {
						if (res.confirm) {
							console.log('用户点击确定');
							_this.aaa = false
							uni.navigateBack()
							// _this.$common.guanbi(`/pages/parameters/parameters?title=${this.title}`)
							
							// _this.$common.navigateBack({delta:2})
						} else if (res.cancel) {
							
							console.log('用户点击取消');
						}
					}
				});
			　　return true;
			}
			// #endif
		},
		methods: {
			pageScrollTo() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				});
			},
			// 获取用户信息 / 获取部门、业务员
			getUserInfo() {
				this.$http.post('Login/GetUsers',{name:this.$db.get('checkboxMobile')},true).then(res => {
					if(res.statusCode = 200) {
						if( res.data.length > 0) {
							this.salesmanName = res.data[0]
						}
					}
				})
			},
			// 获取商品列表
			getData() {
				let sql = `select * from commodity2  limit ${(this.pageIndex-1) * 10},10`
				let temp = 'where 1=1 ';
				if(this.Name != ''){
					temp += `and name like '%${this.Name}%' ` ;
				}
				if(this.InventoryClass.Code != ''){
					temp += `and inventoryClass like '%${this.InventoryClass.Code}%' `;
				}
				sql = `select * from commodity2 ${temp} limit ${(this.pageIndex-1) * 10},10`
				
				if(this.$db.get('default_config').inventory_scope == 1) {
					sql = `select * from commodity2 where priuserdefdecm6 like '%1%' limit ${(this.pageIndex-1) * 10},10`
				}
				// 获取商品数据
				executeSQL('xiaoap', sql, res => {
					res.forEach(e => {
						if(e.inventoryClass) {
							e.inventoryClass = JSON.parse(e.inventoryClass)
						}
						if(e.productInfo) {
							e.productInfo = JSON.parse(e.productInfo)
						}
						if(e.ExistingQuantityList != 'undefined') {
							if(e.ExistingQuantityList.length > 0) {
								e.ExistingQuantityList = JSON.parse(e.ExistingQuantityList)
								e.ExistingQuantityList.forEach(q => {
									if(this.warehouse[this.wareIndex].ID == q.warehouseCode) {
										e.ExistingQuantity = q.existingQuantity
									}
								})
							}
						}
						if(e.partnerUnitPrice) {
							e.partnerUnitPrice = JSON.parse(e.partnerUnitPrice)
							e.partnerUnitPrice.forEach(j => {
								if(this.client.priceGrade) {
										if(this.client.priceGrade.name == '普通客户价') {
											j.Price = j.PriceObj.retailPrice
										}else if(this.client.priceGrade.name.indexOf('一级') != -1) {
											j.Price = j.PriceObj.invSCost1
										}else if(this.client.priceGrade.name.indexOf('二级') != -1) {
											j.Price = j.PriceObj.invSCost2
										}else if(this.client.priceGrade.name.indexOf('三级') != -1) {
											j.Price = j.PriceObj.invSCost3
										}else if(this.client.priceGrade.name.indexOf('四级') != -1) {
											j.Price = j.PriceObj.invSCost4
										}else if(this.client.priceGrade.name.indexOf('五级') != -1) {
											j.Price = j.PriceObj.invSCost5
										}else if(this.client.priceGrade.name.indexOf('六级') != -1) {
											j.Price = j.PriceObj.invSCost6
										}else if(this.client.priceGrade.name.indexOf('最低') != -1) {
											j.Price = j.PriceObj.invLSPrice
										}else if(this.client.priceGrade.name.indexOf('会员价') != -1) {
											j.Price = j.PriceObj.memberPrice
										}else if(this.client.priceGrade.name.indexOf('采购价') != -1) {
											j.Price = j.PriceObj.purchasePrice
										}
								}
							})
							if(e.partnerUnitPrice.length == 2) {
								if(this.$db.get('default_config').unit == 2) {
									e.partnerUnitPrice[1].count = true
									e.partnerUnitPrice[0].count = false
									e.Price = e.partnerUnitPrice[1].Price
									e.RetailPriceNew = e.partnerUnitPrice[1].RetailPriceNew
								}else {
									e.partnerUnitPrice[0].count = true
									e.partnerUnitPrice[1].count = false
									e.Price = e.partnerUnitPrice[0].Price
									e.RetailPriceNew = e.partnerUnitPrice[0].RetailPriceNew
								}
							}
						}
					})
					
					if(this.pageIndex>1){
						this.status = 'loading'
						this.goodsList = this.goodsList.concat(res||[])
					}else{
						this.status = 'loading'
						this.goodsList = res;
					}
					if(res.length==0){
						if(this.pageIndex > 1) {
							this.status = 'noMore';
							this.more= true;
							return;
						}else {
							this.more= false;
						}
					}
				})
				// let resData = {
				// 	param:{
				// 		pageSize:500,
				// 	}
				// }
				// if(this.InventoryClass.Code != '') {
				// 	resData.param.InventoryClass = this.InventoryClass
				// }
				// if(this.Name != '') {
				// 	resData.param.Name = this.Name
				// }
				// this.$http.post('valet_order/inventory_lists',resData).then(res => {
				// 	if(res.code == 1) {
				// 		if(res.data.length > 0) {
				// 			let list = []
				// 			res.data.forEach(e => {
				// 				e.isStatus = false;
				// 				e.checked = false;
				// 				e.number = 0;
				// 				e.giftChecked = false;
				// 				if(e.partnerUnitPrice[0]) {
				// 					if(e.partnerUnitPrice.length > 0) {
				// 						e.partnerUnitPrice.forEach((j,index) => {
				// 							j.count = false
				// 						})
				// 					}
				// 					if(this.$db.get('default_config').unit == 2) {
				// 						e.partnerUnitPrice[1].count = true
				// 					}else {
				// 						e.partnerUnitPrice[0].count = true
				// 					}
				// 					e.Price = e.partnerUnitPrice[0].Price
				// 					e.UnitName = e.partnerUnitPrice[0].UnitName
				// 					e.ProductionDate = this.$common.timeToDate()
				// 					let list = []
				// 					list = this.$common.timeToDate().split('-')
				// 					e.Batch = list[0]+list[1]+list[2]
				// 				}
				// 			})
				// 			if(this.$db.get('default_config').inventory_scope == 1) {
				// 				res.data.forEach(e => {
				// 					if(e.priuserdefdecm6 == 1) {
				// 						list.push(e)
				// 					}
				// 				})
				// 				this.goodsList = list
				// 			}else {
				// 				this.goodsList = res.data
				// 			}
				// 		}
				// 	}
				// })
			},
			kehuClick(i,item) {
				this.kehuList[i].stair = !this.kehuList[i].stair
				this.kehuList[i].sub.forEach(e => {
					if(item.Name == e.Name) {
						this.kehuList[i].stair = !this.kehuList[i].stair
					}else {
						e.headset = false
					}
				})
				
			},
			stairClick(i,j,item) {
				// this.kehuList[i].sub.forEach(e => {
				// 	if(item.Name == e.Name) {
				// 		this.kehuList[i].sub[j].headset = !this.kehuList[i].sub[j].headset
				// 	}else {
				// 		e.headset = false
				// 	}
				// })
				if(item) {
					this.InventoryClass = {
						Code:item.code,
						Name:item.name
					}
				}else {
					this.InventoryClass = {
						Code:'',
						Name:''
					}
				}
				this.classify = false
				this.pageIndex = 1
				this.getData()
			},
			subset(item) {
				if(item) {
					this.InventoryClass = {
						Code:item.code,
						Name:item.name
					}
				}else {
					this.InventoryClass = {
						Code:'',
						Name:''
					}
				}
				this.classify = false
				this.pageIndex = 1
				this.getData()
			},
			// 销售单位切换
			unitsClick(item2,i,k,item) {
				this.goodsList[i].Price = this.goodsList[i].partnerUnitPrice[k].Price
				this.goodsList[i].UnitName = this.goodsList[i].partnerUnitPrice[k].UnitName
				this.goodsList[i].partnerUnitPrice.forEach(e => {
					e.count = false
					item2.count = true
				})
				
				if(this.goodsList[i].isStatus == true) {
					this.SaleOrderDetails.forEach((k , index) => {
						if(k.Inventory.Code == item.code) {
							this.total -= (Number(k.OrigTaxPrice) * Number(k.Quantity))
							k.OrigTaxPrice = item2.Price
							k.Unit.Name = item2.UnitName
							this.total += (Number(k.OrigTaxPrice) * Number(k.Quantity))
						}
					})
					this.total = this.total.toFixed(2)
				}
			},
			// 仓库列表切换
			wareIndexChange(e) {
				this.wareIndex = e.detail.value
			},
			// 减法
			jianClick(item,i,number) {
				this.SaleOrderDetails.forEach((k , index) => {
					if(k.Inventory.Code == item.code) {
						if(number != Number(this.goodsList[i].priuserdefdecm1 || 1)){
							k.Quantity = Number(this.goodsList[i].number) + (Number(this.goodsList[i].priuserdefdecm1  || 1))
						}
						this.goodsList[i].number ++;
					}
				})
				if(this.goodsList[i].number == 0){
					this.goodsList[i].isStatus = false
					this.SaleOrderDetails.forEach((k , index) => {
						if(k.Inventory.Code == item.code) {
							this.SaleOrderDetails.splice(index,1)
						}
					})
				}
			},
			// 加法
			jiaClick(item,i,number) {
				if(number == 0) {
					this.goodsList[i].isStatus = true
					this.SaleOrderDetails.push({
						Inventory:{Code:item.code},
						Unit:{Name:this.goodsList[i].UnitName},
						Quantity:item.number,
						OrigTaxPrice:this.goodsList[i].Price,
						Name:this.goodsList[i].name
						// Batch:this.goodsList[i].Batch,
						// ProductionDate:this.goodsList[i].ProductionDate
					})
				}
				this.SaleOrderDetails.forEach((k , index) => {
					if(k.Inventory.Code == item.code) {
						this.goodsList[i].number --;
						k.Quantity = this.goodsList[i].number
					}
				})
				
			},
			// 销售单进入详情
			inventoryClick(item) {
				if(this.title == '销售订单') {
					this.$common.url(`/pages/product/details?data=${JSON.stringify(item)}`)
				}
			},
			// 单个商品选中
			// checkboxChange(i) {
			// 	this.goodsList[i].checked = !this.goodsList[i].checked
			// 	var list = []
			// 	this.goodsList.forEach(e => {
			// 		if(e.checked) {
			// 			list.push(e)
			// 		}
			// 	})
			// },
			// 选中添加
			addClick(item,i) {
				if(item.number < 0) {
					uni.showToast({
						title: '添加商品成功',
						duration: 500
					});
					let obj = {}
					let aa = 0
					let bb = 0
					if(this.$db.get('shoppingTrolley_1')) {
						obj = this.$db.get('shoppingTrolley_1')
						if(obj.dto.linkMan == this.client.name) {
							for(var i=0; i<obj.dto.saleOrderDetails.length; i++) {
								if(obj.dto.saleOrderDetails[i].Inventory.Code == item.code) {
									if(!item.isPresent && !obj.dto.saleOrderDetails[i].isPresent) {
										obj.dto.saleOrderDetails[i].Quantity += Number(item.number)
										bb++
									}
								}
								aa++
							}
							if(aa == 0 || bb == 0) {
								let value = 0
								if(item.isPresent != true) {
									value = item.Price
								}
								obj.dto.saleOrderDetails.push({
									Inventory: {Code:item.code},
									Name:item.name,
									OrigTaxPrice:value,
									Quantity:Number(item.number),
									Unit: {Name:item.UnitName},
									isPresent:item.isPresent
								})
							}
						}else {
							this.SaleOrderDetails.push({
								Inventory: {Code:item.code},
								Name:item.name,
								OrigTaxPrice:item.Price,
								Quantity:Number(item.number),
								Unit: {Name:item.UnitName},
								isPresent:item.isPresent
							})
						}
						this.$db.set('shoppingTrolley_1',obj)
					}else {
						this.SaleOrderDetails.push({
							Inventory: {Code:item.code},
							Name:item.name,
							OrigTaxPrice:item.Price,
							Quantity:Number(item.number),
							Unit: {Name:item.UnitName},
							isPresent:item.isPresent
						})
					}
				}else {
					this.$common.show('商品数量必须大于0',true)
				}
			},
			bindPickerChange(e) {
				this.index = e.detail.value
				if(this.index == 0) {
					this.Code = ''
				}else {
					this.Name = ''
				}
			},
			 // 开启弹窗
			dateOpen (key, date,value) {
				this.valueStatus = value
				this.dateKey = key
				this.$refs.dateTimePop.open(date || '');
			},
			// 关闭弹窗
			timeOk (str, obj) {
				this.goodsList[this.valueStatus].ProductionDate = str
				this[this.dateKey] = str || ''
			},
			// 地址选择器
			changeClick(e) {
				let list = ''
				e.data.forEach(e => {
					list += e.name
				})
				this.client.partnerAddresDTOs[0].shipmentAddress = list
			},
			// 明细弹框
			changeVisble () {
				if(this.character) {
					this.slide = true
					setTimeout(() =>{
						this.character = false
					},300)
				}else {
					this.slide = false
					this.character = true
				}
			},
			// 明细关闭
			disposition(){
				this.slide = false
				this.character = false
			},
			// 提交订单
			submit() {
				let time = new Date().getTime()+''
				time = time.slice(2,12)
				let _this = this
				if(!this.$db.get('shoppingTrolley_1') && this.SaleOrderDetails.length == 0) {
					return this.$common.show('请选择商品',true)
				}
				if(this.VoucherDate == '') {
					return this.$common.show('请选择单据日期',true)
				}
				if(this.closeAccount.name == '请选择结算客户') {
					return this.$common.show('请选择结算客户',true)
				}
				let resData = {
					dto:{
						voucherDate:this.VoucherDate,
						customer:{
							code:this.client.code 
						},//客户编码
						address:this.client.partnerAddresDTOs[0].shipmentAddress || '',
						linkMan:this.client.name,
						contactPhone:this.client.partnerAddresDTOs[0].mobilePhone || '',
						customerPhone:this.closeAccount.partnerAddresDTOs[0].mobilePhone || '',
						memo:this.Memo,
						externalCode:time,
					},
				}
				if(this.salesmanName) {
					resData.dto.clerk = {
						code:this.salesmanName.personCode 
					}
				}
				let list = []
				resData.dto.dynamicPropertyKeys = ["priuserdefnvc7"]
				resData.dto.dynamicPropertyValues = [this.modePayment || '未付款']
				resData.dto.settleCustomer = {code:this.closeAccount.code}
				resData.dto.warehouse = {code:this.warehouse[this.wareIndex].Code}
				resData.dto.saleDeliveryDetails = this.SaleOrderDetails
				resData.dto.businessType = {code:'16'}
				if(!this.$db.get('shoppingTrolley_1') || this.$db.get('shoppingTrolley_1').dto.linkMan != this.client.name) {
					this.$db.set('shoppingTrolley_1',resData)
				}
				this.SaleOrderDetails = []
				uni.navigateTo({
					url: `/pages/aDraftList/details`,
					success: function(res) {
						setTimeout(() => {
							uni.$emit('page-popup', {
								content: _this.client,
								type:1,
								count:2
							})
						}, 500)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.wanzxinx{
		margin: 0 auto;
		width: 216rpx;
		border-radius: 56rpx;
		border: 2rpx solid #dcdcdc;
		height:56rpx;
	}
	.sousuo {
		height: 60rpx;
		border: 2rpx solid #E7E7EA;
	}
	.countClass{
		background: #3987C3 !important;
		color: #fff !important;
		border: 2rpx solid  #3987C3 !important;
	}
	.daiClass{
		padding: 0 10rpx;
		height: 50rpx;
		box-sizing: border-box;
		background: #fff;
		color: #64acff;
		border: 2rpx solid #64acff;
	}	
	.yuanxing{
		width: 40rpx;
		height: 40rpx;
		border: 2rpx solid #F1F1F1;
		border-radius: 50%;
	}
	/deep/ .label{
		bottom: 80rpx !important;
		margin-bottom: env(safe-area-inset-bottom) !important;
	}
	/deep/ .tankuang{
		bottom: 80rpx !important;
		margin-bottom: env(safe-area-inset-bottom) !important;
	}
</style>